/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.dusa.worklog.viewer;

import cz.dusa.worklog.controller.WorklogDataException;
import cz.dusa.worklog.controller.WorklogDataListener;
import cz.dusa.worklog.controller.WorklogDataManager;
import cz.dusa.worklog.datalib.Project;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JOptionPane;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.Exceptions;
import org.openide.util.ImageUtilities;
import org.openide.util.NbBundle;

/**
 *
 * @author vdusa
 */
public class ProjectNode extends AbstractNode implements WorklogDataListener<Project> {
    private Project project;
    private static final String ICON_PATH = "cz/dusa/worklog/viewer/FolderEdit.png";

    ProjectNode(Project project) {
        super(Children.LEAF);
        setProject(project);
        setIconBaseWithExtension(ICON_PATH);
        WorklogDataManager.getInstance().addWorklogDataListener(this, Project.class, project.getId());
    }

    @Override
    public Image getIcon(int type) {
        return ImageUtilities.loadImage(ICON_PATH);
    }

    @Override
    public Image getOpenedIcon(int type) {
        return getIcon(type);
    }

    @Override
    public String getDisplayName() {
        return getName();
    }

    @Override
    public Action[] getActions(boolean context) {
        Action[] result = new Action[]{
            new OpenAction(),
            new DeleteAction()
        };
        return result;
    }

    @Override
    public Action getPreferredAction() {
        return new OpenAction();
    }

    /**
     * @return the project
     */
    public Project getProject() {
        return project;
    }

    /**
     * @param project the project to set
     */
    public void setProject(Project project) {
        this.project = project;
        setName(project.getName());
    }

    public void afterCreate(Project project) {
        //do nothing
    }

    public void afterUpdate(Project project) {
        setProject(project);
    }

    public void beforeDelete(Project project) {
        //do nothing
    }

    private final class DeleteAction extends AbstractAction {

        public DeleteAction() {
            putValue(Action.NAME, NbBundle.getMessage(ProjectEditorTopComponent.class, "Delete"));
        }

        public void actionPerformed(ActionEvent e) {
            if(JOptionPane.showConfirmDialog(null, NbBundle.getMessage(CustomersTopComponent.class, "DeleteProjectConfirmDialogText"), NbBundle.getMessage(CustomersTopComponent.class, "DeleteProjectConfirmDialogTitle"), JOptionPane.YES_NO_OPTION)!=JOptionPane.YES_OPTION)
                return;

            ProjectEditorTopComponent win = ProjectEditorTopComponent.findInstance(project);
            if (win.isOpened()){
                win.close();
            }
            
            try {
                WorklogDataManager.getInstance().remove(project);
            } catch (WorklogDataException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }

    private final class OpenAction extends AbstractAction{
        public OpenAction() {
            putValue(Action.NAME, NbBundle.getMessage(ProjectEditorTopComponent.class, "Open"));
        }

        public void actionPerformed(ActionEvent e) {
            WorklogWindowManager.getInstance().openWindow(project);
        }
    }

}
